package com.qitmiaojie.test;


import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.file.FileMode;
import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.AppSigner;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.util.Base64Utils;
import org.springframework.util.FastByteArrayOutputStream;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

// 行程码识别 Java示例代码
public class RequestDemo {

    public static void main(String[] args) throws Exception {
        BufferedImage image = ImageIO.read(new FileInputStream(new File("D:\\213124124\\1.jpg")));
        FastByteArrayOutputStream fos = new FastByteArrayOutputStream();
        ImageIO.write(image,"jpg",fos);
        String base64Code = Base64.encodeBase64String(fos.toByteArray());

        String path = "//ocrxingchengma";
        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);
        request.setCredentials("ea8e1cd70f1b42d4be4de5ca85813075",
                "b51d8ee57d82494e9fc76772fbc9f448");

        System.out.println(base64Code);
        request.addHeaderParameter("Content-Type", "application/x-www-form-urlencoded");
        request.setJsonBody(base64Code);


        ApiExplorerClient client = new ApiExplorerClient(new AppSigner());

        try {
            ApiExplorerResponse response = client.sendRequest(request);
            // 返回结果格式为Json字符串
            System.out.println(response.getResult());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
